<?php
    $message = $this->message;
    $auth = $this->auth;
?>
<!-- alert -->
<div class="class=sortable row-fluid ui-sortable" id="">
    <div class="box-content alerts">
        <div class="alert alert-success" id="alert">

        </div>
    </div>
</div>
<!-- alert -->
<div class="row-fluid sortable ui-sortable">
    <div class="box span12">
        <div class="box-header well" data-original-title="">
            <h2>Striped Table</h2>
            <div class="box-icon">
                <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
                <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
                <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
            </div>
        </div>
        <div class="box-content">
            <table class="table table-striped">
                <thead>

                <tr>
                    <th>From User Name</th>
                    <th>Content </th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>

                </thead>
                <tbody>
                <?php
                foreach($message as $m){
                ?>
                <tr>
                    <td><?php echo $m->from_user_name; ?></td>
                    <td class="center"><?php echo $m->message_content ?></td>
                    <td class="center">
                        <?php
                       $result = \Velacolib\Utility\Utility::Messge_status_class($m->is_accepted);
                        ?>
                        <span class="<?php echo $result['class'] ?>"><?php echo $result['type'] ?></span>
                    </td>
                    <td class="center">
                        <button class="bt_Accept btn btn-mini btn-primary" data-message-id="<?php echo $m->id ?>" data-friend-id="<?php echo $m->form_user ?>" data-user-id="<?php echo $m->to_user ?>" data-follow="<?php echo $m->type_request ?>" >Accept</button>
                        <button class="btn btn-mini btn-danger  btn_delete" data-message-id="<?php echo $m->id; ?>">Delete</button>
                    </td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
            <!--paginator here -->
            <?php // add at the end of the file after the table
            echo $this->paginationControl(
            // the paginator object
                $message,
                // the scrolling style; see http://zf2.readthedocs.org/en/release-2.1.4/modules/zend.paginator.usage.html#rendering-pages-with-view-scripts
                'sliding',
                // the partial to use to render the control
                array('partial/paginator.phtml', 'User'),
                // the route to link to when a user clicks a control link
                array(
                    'route' => 'admin/child',
                    'child_route' => array('controller'=>'user', 'action'=>'loademail','id'=>$auth->userid)
                )
            );
            ?>
            <!--paginator here -->
        </div>
    </div><!--/span-->
</div>

<script>
    $(document).ready(function(){
        $('#alert').hide();
        //accept one request
        $('.bt_Accept').click(function(){

           var friend_id = $(this).attr('data-friend-id');
           var user_id = $(this).attr('data-user-id');
           var follow = $(this).attr('data-follow');
           var message_id = $(this).attr('data-message-id');

            $.ajax({
                url: '<?php echo $this->url('admin/child',array('controller'=>'user','action'=>'acceptfriend')) ?>',
                type: 'post',
                data : {user_id: user_id,friend_id: friend_id, follow: follow, message_id: message_id }
            }).done(function(data){
                    if(data == 0){
                        var content = '<button type="button" class="close" data-dismiss="alert">×</button> <strong>Well Done</strong> 1 new user add to your friend list';

                        $('#alert').addClass('alert-success');
                    }
                    else{
                        var content = '<button type="button" class="close" data-dismiss="alert">×</button> <strong>Oops Done</strong> User existing in your friend list';
                        $('#alert').addClass('alert-error');
                    }
                    $('#alert').show();
                    $('#alert').empty();
                    $('#alert').append(content);
            })
        })

        //delete message
        $('.btn_delete').click(function(){
            var message_id = $(this).attr('data-message-id');
            $.ajax({
                url: '<?php echo $this->url('admin/child',array('controller'=>'user','action'=>'loademail')) ?>',
                type: 'post',
                data : {message_id: message_id }
            }).done(function(data){
                    location.reload();
                });
        })
    })
</script>